package cn.action;

import cn.entity.img.bigSword;
import cn.entity.master.Fighter;
import cn.entity.utils.Paint;
import cn.entity.img.Sword;

import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Set;


public class AttackAction {
    private Fighter fighter;
    private boolean isCharging = false;
    private boolean wasMoving = false;

    public AttackAction(Fighter fighter) {
        this.fighter = fighter;
    }

    public void attackStart(int k, Set<Integer> pressedKeys, Fighter fighter) throws IOException {
        if(k == KeyEvent.VK_J){
            if(Paint.Sword.isEmpty() && Paint.a != 3){
                Paint.Sword.add(new Sword(fighter.getX(), fighter.getY()));
            }
        }
        else if(k == KeyEvent.VK_K){
            if(Paint.a != 3){
                if(fighter.setPower()){
                    Paint.flySword.add(new Sword(fighter.getX(), fighter.getY()));
                }
            }
        }
        else if(k == KeyEvent.VK_L){
            if(Paint.a != 3 && !isCharging){
                isCharging = true;
                fighter.setBigSword(1);
                wasMoving = isMoving(pressedKeys);
            }
        }
        else if(k == KeyEvent.VK_O){
            fighter.upPower();
            fighter.setState(false);
        }


    }

    public void attackStop(int k) throws IOException {
        if(k == KeyEvent.VK_J){
            for (int i = 0; i < Paint.Sword.size(); i++) {
                Paint.Sword.remove(i);
            }
        }
        else if(k == KeyEvent.VK_O){
            fighter.setState(true);
        }
        else if(k == KeyEvent.VK_L && isCharging){
            if(fighter.getBigSwordValue() >= 25) {
                Paint.bigSword.add(new bigSword(fighter.getX(), fighter.getY()));
                Paint.bigSword.get(Paint.bigSword.size()-1).startAttackAnimation();
                fighter.resetBigSword();
            }
            isCharging = false;
        }
    }


    private boolean isMoving(Set<Integer> pressedKeys) {
        return pressedKeys.contains(KeyEvent.VK_W) ||
                pressedKeys.contains(KeyEvent.VK_A) ||
                pressedKeys.contains(KeyEvent.VK_S) ||
                pressedKeys.contains(KeyEvent.VK_D);
    }
}